package com.example.jd.controller;

import com.example.jd.dao.IGoodsDao;
import com.example.jd.pojo.Cart;
import com.example.jd.pojo.Goods;
import com.example.jd.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class GoodsHandler {

    @Resource
    IGoodsDao goodsDao;

    /**
     * 返回查询到的商品信息给前端HTML
     *
     * @param page  页码
     * @param limit 每页显示的商品数量
     * @return 返回商品列表
     */
    @RequestMapping("/goods")
    PageInfo<Goods> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "8") int limit) {
        //实现商品的分页展示
        PageHelper.startPage(page, limit);
        List<Goods> list = goodsDao.goods();
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 添加商品进入购物车
     *
     * @param id 商品id
     * @return 返回状态码
     */
    @RequestMapping("/addcart")
    int addcart(int id, HttpSession session) {
        System.out.println("用户正在加入购物车: " + session.getAttribute("user"));
        System.out.println("加入购物车的商品id是: " + id);
        //从session中取出保存的用户信息
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return -2;//表示用户未登入
        }
        //获取当前用户购物车的数据
        List<Cart> carts = goodsDao.getCarts(user.getId());
        for (Cart cart : carts) {
            if (cart.getGoodsid() == id) {//改商品已存在
                goodsDao.changeGoodsNum(user.getId(), id, cart.getNum() + 1);
                return 0;
            }
        }
        //拼接购物车数据，增加进数据库中
        Cart cart = new Cart();//购物车对象
        cart.setUserid(user.getId());
        cart.setGoodsid(id);
        //要从商品表中查询出商品的价格
        Goods goods = goodsDao.getGoodById(id);
        if (null == goods) {
            return -1;//表示商品不存在
        }
        cart.setGoodsprice(goods.getPrice());
        cart.setNum(1);
        //加入购物车
        goodsDao.addcart(cart);
        return 0;
    }

    @RequestMapping("/getCarts")
    List<Cart> getCarts(HttpSession session) {
        User user = (User) session.getAttribute("user");
        //判断登入
        if (null == user) {
            return null;
        }
        //根据用户id得到用户数据
        List<Cart> carts = goodsDao.getCarts(user.getId());
        return carts;
    }

    @RequestMapping("/delcart")
    int delcart(int id, HttpSession session) {
        User user = ((User) session.getAttribute("user"));
        if (user == null) {
            return 1;
        }
        goodsDao.delcart(user.getId(), id);
        return 0;
    }

    @RequestMapping("/updateGoodsNum")
    int updateGoodsNum(int id, int t, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        List<Cart> carts = goodsDao.getCarts(userId);
        Cart cart1 = null;
        for (Cart cart : carts) {
            if (id == cart.getGoodsid()) {
                cart1 = cart;
            }
        }
        Integer num = cart1.getNum();
        if (t == 0) {
            if (num - 1 == 0) {
                goodsDao.delcart(user.getId(), id);
                return 0;
            } else
                goodsDao.changeGoodsNum(userId, id, num-1);
        } else if (t == 1) {
            goodsDao.changeGoodsNum(userId, id, num+1);
        }
        return 1;
    }
}
